package com.huadi.cmcc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huadi.cmcc.domain.Result;
import com.huadi.cmcc.domain.User;
import com.huadi.cmcc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author zhw
 * @date 2021/8/31
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/create")
    public Result create(User user) {
        user.setId(null);
        userService.save(user);
        return Result.success();
    }


    @GetMapping("/page")
    public Result<Page<User>> page(Page<User> page, String name) {
        Wrapper<User> query = Wrappers.<User>lambdaQuery()
                .like(StrUtil.isNotBlank(name), User::getName, name);
        final Page<User> resultPage = userService.page(page, query);
        return Result.success(resultPage);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        userService.removeById(id);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(User user) {
        userService.updateById(user);
        return Result.success();
    }

    @PostMapping("/nowUpdate")
    public Result nowUpdate(User user, HttpSession session) {
        final User s = (User) session.getAttribute("user");
        if (s == null) {
            return Result.fail("用户未登录");
        }
        final Long id = s.getId();
        user.setId(id);
        userService.updateById(user);
        return Result.success(user);
    }

    @PostMapping("/login")
    public Result<User> login(String username, String password, HttpSession session) {
        Wrapper<User> query = Wrappers.<User>lambdaQuery()
                .eq(User::getName, username)
                .eq(User::getPassword, password)
                .last("limit 1");
        final User user = userService.getOne(query);
        if (user == null) {
            return Result.fail("用户名密码错误");
        }
        session.setAttribute("user", user);
        return Result.success(user);
    }

    @PostMapping("/register")
    public Result<User> register(User user) {
        Wrapper<User> query = Wrappers.<User>lambdaQuery()
                .eq(User::getName, user.getName())
                .last("limit 1");
        final User one = userService.getOne(query);
        if (one != null) {
            return Result.fail("用户名已经存在");
        }
        userService.save(user);
        return Result.success(user);
    }

    @PostMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("user");
        return Result.success();
    }

    @GetMapping("now")
    public Result<User> getNow(HttpSession session) {
        final User user = (User) session.getAttribute("user");
        if (user == null) {
            System.out.println("用户未登录");
            return Result.fail("用户未登录");
        }
        return Result.success(user);
    }

    @GetMapping("nowList")
    public Result<Page<User>> getNowList(Page<User> page, HttpSession session) {
        final User user = (User) session.getAttribute("user");
        if (user == null) {
            System.out.println("用户未登录");
            return Result.fail("用户未登录");
        }
        Wrapper<User> query = Wrappers.<User>lambdaQuery()
                .eq(User::getId, user.getId());
        return Result.success(userService.page(page, query));
    }
}
